'use strict';

import Base from './base.js';

export default class extends Base {
    /**
     * index action
     * @return {Promise} []
     */
    async indexAction() {
        let auths = [];
        let auth0s = await this.model("s_auth").where({parentId: 0}).select();
        for (let i in auth0s) {
            let auth0 = auth0s[i];
            let auth = {
                authKey: auth0.id,
                label: auth0.name,
                children: []
            };
            let auth1s = await this.model("s_auth").where({parentId: auth0.id}).select();
            for (let j in auth1s) {
                let auth1 = auth1s[j];
                auth.children.push({
                    authKey: auth1.id,
                    label: auth1.name
                })
            }
            auths.push(auth);
        }
        return this.success(auths);
    }

    async roleAction() {
        let roleId = this.get("roleId");
        let auths = await this.model("s_auth").join("s_role_auth on s_auth.id = s_role_auth.authId").where({roleId: roleId}).select();
        return this.success(auths);
    }

}